package com.whtq.front.common.aspect;

import com.whtq.front.common.basics.WHTQReturnCode;
import com.whtq.front.common.exception.ParamsException;
import com.whtq.front.common.exception.WHTQException;
import com.whtq.front.common.model.response.WHTQRes;
import com.whtq.front.common.model.response.WHTQResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONException;
import org.apache.ibatis.javassist.NotFoundException;

/**
 * Created by
 *
 * @author wmi
 * @date 2021/9/14 - 11:46.
 * @description
 */
@ControllerAdvice
@Slf4j
public class ResponseResultHandler implements ResponseBodyAdvice<Object> {

    public static final String RESPONSE_RESULT = "RESPONSE-RESULT-WHTQ";

    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        /**
         * 判断请求是否有包装标记
         */
        WHTQResult responseResult = (WHTQResult) request.getAttribute(RESPONSE_RESULT);
        return responseResult == null ? false:true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType,
                                  Class<? extends HttpMessageConverter<?>> aClass,
                                  ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        /**
         * 进入返回体包装格式
         */
        return WHTQRes.success(o);
    }



    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.BAD_GATEWAY)
    @ResponseBody
    public WHTQRes handleException(HttpServletRequest request, HttpServletResponse response, final Exception e) {
        WHTQRes whtqRes = new WHTQRes(WHTQReturnCode.SYSTEM_ERROR);
//        whtqRes.setMsg("系统开小差了, 程序猿修复中...");
//        log.error("统一异常处理");
//        log.error("异常信息：{}", e);
        if(e instanceof com.whtq.front.common.exception.WHTQException){
            return WHTQRes.failure(((com.whtq.front.common.exception.WHTQException) e).getWhtqReturnCode());
        }else if(e instanceof Exception){
            log.error("异常信息：{}", e);
        }
        return whtqRes;
    }

    @ExceptionHandler(value = NotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    public WHTQRes notFoundException(Object handler, Exception e) {
        WHTQRes whtqRes = new WHTQRes(WHTQReturnCode.NO_RESOURCE);
        e.printStackTrace();
        return whtqRes;
    }

    @ExceptionHandler(value = {JSONException.class, ParamsException.class})
    @ResponseBody
    public WHTQRes paramErrorException(Object handler, Exception e) {
        WHTQRes whtqRes = new WHTQRes(WHTQReturnCode.PARAMS_ERROR);
        e.printStackTrace();
        return whtqRes;
    }

    @ExceptionHandler(WHTQException.class)
    @ResponseBody
    public WHTQRes businessExceptionHandler(Object handler, WHTQException e) {
        WHTQRes whtqRes = new WHTQRes();
        whtqRes.setCode(e.getCode());
        whtqRes.setMsg(e.getMessage());
        return whtqRes;
    }


}
